package com.gec.dao.Impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.gec.dao.BuycarDao;
import com.gec.entity.Buycar;

public class BuycarDaoImpl extends BaseDaoImpl<Buycar> implements BuycarDao {
	
	String sql = "select b.bc_id,b.bc_mem_id,b.bc_good_id,b.bc_qty,g.good_img,g.good_name,g.good_price,g.good_curPrice "
			+ "from fs_buycars b join fs_goods g on b.bc_good_id = g.good_id join fs_goodtypes gt on "
			+ "gt.gtype_id= g.good_type_id ";
	
	@Override
	public void insert(Buycar a) {
		String sql = "insert into fs_buycars values(default,?,?,?)";
		this.execUpdate(sql, a.getMem_id(),a.getGood_id(),a.getQty());		
	}
	
	@Override
	public void addbc(Buycar bc) {
		String sql = "insert into fs_buycars values(default,?,?,1)";
		this.execUpdate(sql, bc.getMem_id(),bc.getGood_id());
	}

	@Override
	public int update(Buycar a) {
		String sql = "update fs_buycars set bc_qty = bc_qty+1  where bc_mem_id = ? and bc_good_id = ?";
		return this.execUpdate(sql, a.getMem_id(),a.getGood_id());
	}

	@Override
	public int delete(Integer id) {
		return this.execUpdate("delete from fs_buycars where bc_id = ?", id);
	}

	@Override
	public Buycar selectById(Integer id) {
		return this.execOne(sql+"where bc_id=?", id);
	}

	@Override
	public List<Buycar> selectAll() {
		return null;
	}

	@Override
	protected Buycar mapper(ResultSet rs) throws SQLException { 
		Buycar bc = new Buycar();
		bc.setId(rs.getInt(1));
		bc.setMem_id(rs.getInt(2));
		bc.setGood_id(rs.getInt(3));
		bc.setQty(rs.getInt(4));
		bc.setGoodImg(rs.getString(5));
		bc.setGoodName(rs.getString(6));
		bc.setGoodPrice(rs.getFloat(7));
		return bc;
	}

	@Override
	public Buycar selectMeId(int id) {
		return this.execOne(sql+"where bc_mem_id = ?", id);
	}

	@Override
	public Buycar selectGoodId(int gid) {
		return this.execOne(sql+"where bc_good_id = ?", gid);
	}

	@Override
	public int total(int id, int sid) {
		  return ((BigDecimal)this.execSingle("select sum(b.bc_qty) from fs_buycars b join fs_goods g on b.bc_good_id = g.good_id "
		  		+ "join fs_goodtypes gt on gt.gtype_id= g.good_type_id where b.bc_mem_id=? and gt.gtype_shop_id=?",id,sid)).intValue();
	}

	@Override
	public Buycar selectId(int mem_id, int good_id) {
		return this.execOne(sql+"where b.bc_mem_id = ? and b.bc_good_id = ?", mem_id,good_id);
	}

	@Override
	public List<Buycar> showAll(int id, int sid) {
		return this.execQuery(sql+"where b.bc_mem_id=?", id);
	}

	@Override
	public Buycar selectNum(int id, int gid) {
		 String baseSql = "select b.*,g.good_img,good_name,good_curPrice from fs_buycars b join fs_goods g "
				   + "on b.bc_good_id=g.good_id join fs_goodtypes t on g.good_type_id = t.gtype_id ";
		return  this.execOne(baseSql+"where b.bc_mem_id = ? and b.bc_good_id = ? ", id,gid);
	}

	@Override
	public Object selectSum(int id, int sid) {
		String sqa = "select sum(bc_qty) from fs_buycars b join fs_goods g on b.bc_good_id = g.good_id "
				+ "join fs_goodtypes t on g.good_type_id = t.gtype_id "
				+ "where b.bc_mem_id = ? and t.gtype_shop_id = ?";
		return this.execSingle(sqa, id,sid);
	}

	@Override
	public int findCurs(int id) {
		//有问题
		return this.execUpdate(sql, id);
	}

	@Override
	public void addsNum(Integer integer) {
		this.execUpdate("update fs_buycars set bc_qty=bc_qty+1 where bc_id=?", integer);
	}

	@Override
	public int selectByIdQty(Integer integer) {
		return (int)this.execSingle("select bc_qty from fs_buycars where bc_id=?", integer);
	}

	@Override
	public List<Buycar> selectAllBuycar(int id, Integer integer) {
		return this.execQuery(sql+"where b.bc_mem_id=? and gt.gtype_shop_id=?", id,integer);
	}

	@Override
	public void cutGoodnum(Integer integer) {
		this.execUpdate("update fs_buycars set bc_qty=bc_qty-1 where bc_id=?", integer);
	}
	
}
